import mConfig from '@/config/base';
// import siteInfo from '@/siteinfo';
import siteInfo from '@/config';
import user from "@/common/user";
import $store from '../store'

class url{
	
	// 独立板的URL
	dl(action , type = 1){
		let get = {
			wid : siteInfo.config.wid,
			ver : mConfig.versionNo,
			// #ifdef MP-WEIXIN
			client_type:1,
			// #endif
			// #ifdef MP-ALIPAY
			client_type:2,
			// #endif
			// #ifdef H5
			client_type:3,
			// #endif
		}
		
		//传送分销的用户分销ID
		if($store.state.$agent_id!="" && $store.state.$agent_id!=undefined && $store.state.$agent_id!=null){
			let agent_id = $store.state.$agent_id;
			get['scene']=agent_id;
		}
		
		if(type == 1 ){
			let token = $store.state.$token;
			if(token!='' && token!=undefined){
				get['token']=token;
			}
		}
		
		//#ifdef MP-WEIXIN || MP-ALIPAY
			return this.wxAppUrl(action,get);
		//#endif	
		
		//#ifdef H5
			return this.h5Url(action,get);
		//#endif
	}
	
	// 微信小程序URL规则
	wxAppUrl(action,get){
		return url= 'https://' + siteInfo.config.domain + '/' + mConfig.modular + '/app/' + action + uni.$u.queryParams(get);
	}
	
	// H5 URL 规则
	h5Url(action,get){		
		let host = window.location.host;
		// 获取地址上的get参数
		let href = window.location.href;
		let reg = new RegExp('[?&]wid=([^&#]+)');
		let wid = href.match(reg);
		if(wid==null || wid==undefined || wid.length==0){
			wid=null;
		}else{
			wid=wid[1];
		}
		if (process.env.NODE_ENV === 'development') {		//开发环境
			host = siteInfo.config.domain;
			wid = siteInfo.config.wid;	
		}
		
		if(wid!=null && wid!=undefined && wid>0){		//有传参数
			get.wid = wid;
			uni.setStorageSync('wid', wid);
		}else{
			//获取缓存wid
			let wid = uni.getStorageSync('wid');
			if(wid!=null && wid!=undefined && wid>0){
				get.wid = wid;
			}
		}
		
		let url= window.location.protocol + "//" + host + '/' + mConfig.modular + '/app/' + action + uni.$u.queryParams(get);
		
		return url;
	}
}
export default new url();